package com.xmg.server.goods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xmg.server.goods.mapper.CatalogMapper;
import com.xmg.shop.api.domain.Catalog;
import com.xmg.shop.api.domain.CatalogProperty;
import com.xmg.shop.api.service.ICatalogPropertyService;
import com.xmg.shop.api.service.ICatalogService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service
public class CatalogServiceImpl implements ICatalogService {
    @Autowired
    private CatalogMapper catalogMapper;
    @Autowired
    private ICatalogPropertyService catalogPropertyService;

    @Override
    public List<Catalog> selectAll() {
        return catalogMapper.selectAll();
    }

    @Override
    public List<Catalog> getChildenCatalogById(Long id) {
        return catalogMapper.getChildenCatalogById(id);
    }

    @Override
    public Catalog selectByPrimaryKey(Long catalogId) {
        return catalogMapper.selectByPrimaryKey(catalogId);
    }

    @Override
    public List<Catalog> getChildNode(long id) {
        return catalogMapper.getChildNode(id);

    }

    @Override
    public List<Catalog> getParentNode() {
        return catalogMapper.getParentNode();
    }

    public void save(String code, String name, Long parentCatalogId) {
        Catalog catalog = new Catalog();
        Date date = new Date();
        catalog.setCode(code);
        catalog.setName(name);
        catalog.setCreateDate(date);
        catalog.setLastModifiedDate(date);
        catalog.setIsParent(Catalog.NO_PARENT);
        catalog.setChildrenCatalogs(0);
        catalog.setParentCatalogId(parentCatalogId);
        //如果新增的不是顶级节点
        if (parentCatalogId != null) {
            Catalog parent = this.selectByPrimaryKey(parentCatalogId);
            //更该为父节点
            parent.setIsParent(Catalog.IS_PARENT);
            //字节点+1
            parent.setChildrenCatalogs(parent.getChildrenCatalogs()+1);
            catalogMapper.updateByPrimaryKey(parent);
        }
        catalogMapper.insert(catalog);
    }

    //删除节点下的所有属性和值
    public void delete(Long id) {
        List<CatalogProperty> catalogPropertys = catalogPropertyService.getCatalogProperty(id);
        for (CatalogProperty catalogProperty : catalogPropertys) {
            catalogPropertyService.delete(catalogProperty.getId());
        }
    }

    @Override
    public void update(Catalog catalog) {
        Catalog updateCatalog = this.selectByPrimaryKey(catalog.getId());
        updateCatalog.setCode(catalog.getCode());
        updateCatalog.setName(catalog.getName());
        updateCatalog.setLastModifiedDate(new Date());
        catalogMapper.updateByPrimaryKey(updateCatalog);
    }

    private List<Long> delete;

    public void deleteChild(Long[] id) {
        delete.addAll(Arrays.asList(id));
        Long[] childs = catalogMapper.selectChildId(id);
        if (childs.length > 0) {
            deleteChild(childs);
        }
    }

    public void deleteChilds(Long[] id){
        delete = new ArrayList<Long>();
        this.deleteChild(id);
        Collections.reverse(delete);
        for (Long childId : delete) {
            this.delete(childId);
        }
        //获取删除的最上层的一个节点对象
        Catalog childCatalog = catalogMapper.selectByPrimaryKey(delete.get(delete.size() - 1));
        //根据这个节点找对对应的父节点
        if(childCatalog.getParentCatalogId()!=null){
            Catalog parentCatalog = catalogMapper.selectByPrimaryKey(childCatalog.getParentCatalogId());
            //减去父节点下的子节点数量
            parentCatalog.setChildrenCatalogs(parentCatalog.getChildrenCatalogs()-1);
            if(parentCatalog.getChildrenCatalogs()==0){
                parentCatalog.setIsParent(Catalog.NO_PARENT);
            }
            catalogMapper.updateByPrimaryKey(parentCatalog);
        }
        catalogMapper.deleteChilds(delete);
    }

    @Override
    public List<Catalog> seleCatalogNameAndId() {
        return catalogMapper.seleCatalogNameAndId();
    }

}
